<%@page import="java.util.UUID"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/pages/common/taglib.jsp" %>
<script type="text/javascript" src="/res/jsLib/wiseMen/workOrder/common.js"></script>
<%
    String uuid = UUID.randomUUID().toString();
    request.setAttribute("uuid", uuid);
%>
<div class="box box-primary">
<div class="row">
	<div class="col-md-12">
		<div class="box-header with-border">
			<h3 class="box-title">
			</h3>
			<div class="box-tools pull-right" id="iWorkOrderTb_${uuid}">
			       来电号码：<input type="text" name="s_ldhm" />
				<button class="btn btn-sm btn-default searchBtn">
	    			<span class="glyphicon glyphicon-search" aria-hidden="true"></span> 搜索
	    		</button>
				<button class="btn btn-sm btn-success addBtn">
	    			<span class="glyphicon glyphicon-plus" aria-hidden="true"></span> 添加
	    		</button>
	    		<button class="btn btn-sm btn-primary editBtn">
	    			<span class="glyphicon glyphicon-pencil" aria-hidden="true"></span> 修改
	    		</button>
	    		<button class="btn btn-sm btn-danger delBtn">
	    			<span class="glyphicon glyphicon-trash" aria-hidden="true"></span> 删除
	    		</button>
	    		<button class="btn btn-sm btn-primary dealBtn">
	    			<span class="glyphicon glyphicon-pencil" aria-hidden="true"></span> 处理
	    		</button>
			</div>
		</div>
		<div class="box-body">
			<table id="iWorkOrderGrid_${uuid}" >
	   		</table>
		</div>
	</div>
</div>
</div>

<script type="text/javascript">
$(function(){
	var uuid = '${uuid}';
	
	var _iworkOrderGridTB=$("#iWorkOrderTb_"+uuid);
    var iworkOrderGrid = $("#iWorkOrderGrid_"+uuid).bootstrapTable({
    	url:ctx + "/workOrderAct/queryWorkOrderDealList.do?clzts=0,2",
    	columns:[
			{field: '_id',checkbox:true},
			{title: '业务类型', field: 'YWLX',formatter:function(data, row, index){
            	return wo.getDataDicItemVal('YWLX', data);
            }},
            {title: '子业务类型', field: 'ZYWLX',formatter:function(data, row, index){
            	return wo.getDataDicItemVal('ZYWLX', data);
            }},
            {title: '来电号码', field: 'LDHM', width: "210px"},
            {title: '客户姓名', field: 'KHXM'},
            {title: '客户诉求', field: 'KHTSBT'},
            {title: '登记时间', field: 'CREATETIME',width: "150px",formatter:function(data, row, index){
            	return webUtils.parseTimestamp(data, 'yyyy-MM-dd HH:mm:ss');
            }},
            {title: '流转时间', field: 'HJSJ',width: "150px",formatter:function(data, row, index){
            	return webUtils.parseTimestamp(data, 'yyyy-MM-dd HH:mm:ss');
            }},
            {title: '当前环节', field: 'HJMC',formatter:function(data, row, index){
            	var html = data;
            	if(row.CLZT=='2'){
            		html+='<font color="red">(缓)</font>';
            	}
            	if(row.CQBJ=='1'){
            		html+='<font color="red" title="重启原因:'+wo.getDataDicItemVal('CQYY', row.CQYY)+'">(重)</font>';
            	}
            	html+='<button type="button" class="btn btn-danger btn-sm"><span class="glyphicon glyphicon-pencil"></span> 修改</button>';
            	return html;
            }}
    	],
	    queryParams: function(d) {
	        d.ldhm = $("input[name=s_ldhm]").val();
	        return d;
	    }
    });
    
    _iworkOrderGridTB.find(".addBtn").click(function(){
    	var uuid=webUtils.uuid();
		var d=editIWrokOrder({
			winId:uuid,
			onremove:function(){
				webUtils.topicPublish("dialog.close."+uuid,{uuid:uuid});
			},
		});
		webUtils.topicSubscribe("dialog.submit.success."+uuid,function(data){
			iworkOrderGrid.bootstrapTable("refresh");
			d.close();
		},true);
    });
    
    _iworkOrderGridTB.find(".editBtn").click(function(){
    	var uuid=webUtils.uuid();
    	var rows=iworkOrderGrid.bootstrapTable("getSelections");
    	if(rows && rows.length>0){
    		var sub = webUtils.topicSubscribe("workOrder.edit.success", function (args) {
        		dialog.close();
        		iworkOrderGrid.bootstrapTable("refresh");
            });
    		var row=rows[0];
    		var d=editIWrokOrder({
    			id:row.ID,
    			winId:uuid,
    			onremove:function(){
    				webUtils.topicPublish("dialog.close."+uuid,{uuid:uuid});
    			},
    		});
    		webUtils.topicSubscribe("dialog.submit.success."+uuid,function(data){
    			iworkOrderGrid.bootstrapTable("refresh");
    			d.close();
    		},true);
    	} else {
    		webUtils.alert("请选择需要编辑的数据");
        }
        
    });
    
    _iworkOrderGridTB.find(".dealBtn").click(function(){
    	var uuid=webUtils.uuid();
    	var rows=iworkOrderGrid.bootstrapTable("getSelections");
    	if(rows && rows.length>0){
    		var sub = webUtils.topicSubscribe("workOrder.edit.success", function (args) {
        		dialog.close();
        		iworkOrderGrid.bootstrapTable("refresh");
            });
    		var row=rows[0];
    		var d=dealIWrokOrder({
    			id:row.ID,
    			clid:row.CLID,
    			winId:uuid,
    			onremove:function(){
    				webUtils.topicPublish("dialog.close."+uuid,{uuid:uuid});
    			},
    		});
    		webUtils.topicSubscribe("dialog.submit.success."+uuid,function(data){
    			iworkOrderGrid.bootstrapTable("refresh");
    			d.close();
    		},true);
    	} else {
    		webUtils.alert("请选择需要编辑的数据");
        }
        
    });
    
    _iworkOrderGridTB.find(".delBtn").click(function(){
    	var rows=iworkOrderGrid.bootstrapTable("getSelections");
    	if(rows && rows.length>0){
    		webUtils.confirm("删除后将不可恢复，请问是否继续?", function(result) {
				if(result){
					var selecteds = [];
	                $.each(rows, function (i, n) {
	                    selecteds.push(n.ID);
	                });
	                $.post(ctx + "/workOrderAct/delWorkOrder.do.do", {ids: selecteds.join(',')}, function (data) {
	                    if (data.success) {
	                    	iworkOrderGrid.bootstrapTable("refresh");
	                    } else {
	                    	webUtils.alert(data.msg);
	                    }
	                }, 'json');
				}
    		});
    		
    	} else {
    		webUtils.alert("请选择需要删除的数据");
        }
    });
    
    _iworkOrderGridTB.find(".searchBtn").click(function(){
    	iworkOrderGrid.bootstrapTable("refresh");
    });
    
    var editIWrokOrder=function(params){
        var url = "/workOrderAct/addWrokOrderView.do?";
    	params = params || {};
        var t = [];
        if (params.id) {
            t.push("id=" + params.id);
            url = "/workOrderAct/editWrokOrderView.do?";
        }
        if (params.winId) {
            t.push("winId=" + params.winId);
        }
        var dialog=webUtils.dialog({
          	 title:'工单登记',
          	 url:ctx + url + t.join("&"),
          	 width:1250,
          	 height:600,
          	 onremove:function () {
                   if (params.onremove) {
                       params.onremove.call(null);
                   }
              }
           });
        return dialog;
    }
    
    var dealIWrokOrder=function(params){
    	params = params || {};
        var t = [];
        if (params.id) {
            t.push("id=" + params.id);
        }
        if (params.clid) {
            t.push("clid=" + params.clid);
        }
        if (params.winId) {
            t.push("winId=" + params.winId);
        }
        
        var dialog=webUtils.dialog({
          	 title:'工单处理',
          	 url:ctx + "/workOrderAct/dealWrokOrderView.do?" + t.join("&"),
          	 width:1250,
          	 height:600,
          	 onremove:function () {
                   if (params.onremove) {
                       params.onremove.call(null);
                   }
              }
           });
        return dialog;
    }
    
});
</script>